//
//  ReadLockStoreView.swift
//  WalkNovel
//
//  Created by Kevin on 2023/8/30.
//

import UIKit

class ReadLockStoreView: UIView {
    
    lazy var scrollView: UIScrollView = {
        let scro = UIScrollView.init(frame: .zero)
        scro.showsHorizontalScrollIndicator = true
        scro.showsVerticalScrollIndicator = false
        return scro
    }()
    
    lazy var allBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setImage(UIImage(named: "setting_right_arrow"), for: .normal)
        btn.rx.tap.bind { [weak self] in
            self?.viewContainingController?.wn_pushStoreVC()
        }.disposed(by: rx.disposeBag)
        return btn
    }()
    
    var storeViewArr = [StoreColCell]()

    var rechargeArr: [RechargeDetailModel]? {
        didSet {
            if let arr = rechargeArr {
                for item in scrollView.subviews {
                    item.removeFromSuperview()
                }
                storeViewArr.removeAll()
                guard arr.count > 0 else { return }
                for (i, v) in arr.enumerated() {
                    let view = StoreColCell()
                    view.frame = CGRect(x: CGFloat(i)*(itemWidth+15)+15, y: 0, width: itemWidth, height: itemHeight)
                    view.buyBtn.rx.tap.bind {
                        WNBuriedManager.shared.logProductItem(v.productId ?? "", v.productName ?? "", v.price)
                        WNPayManager.shared.creatRechargeOrder(v.productId ?? "", v.productName ?? "", v.price)
                    }.disposed(by: rx.disposeBag)
                    view.darkModeConfig()
                    view.tag = i+1
                    view.model = arr[i]
                    storeViewArr.append(view)
                    scrollView.addSubview(view)
                }
                scrollView.contentSize = CGSize(width: CGFloat(arr.count)*(itemWidth+15)+15, height: 0)
            }
        }
    }
    
    func darkModeConfig() {
        for i in storeViewArr {
            i.darkModeConfig()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(scrollView)
        addSubview(allBtn)
        scrollView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        allBtn.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalTo(-6)
            make.width.height.equalTo(24)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
